<?php             
class Menu extends MY_Controller{				
	function __construct()
	{
		parent::__construct();
	}
	function _remap($method,$params = array())
	{
	   $this->_admin_remap($method,$params);	   
	}
        
    function index(){
        $this->load->model('Language');
        $data = array();
        foreach($this->Language->get_all() as $row){
                $data['langList'][$row['language_code']] = $row['language_name'];
        }
    	$this->_setLayout('admin/menu/menu_list','admin_layout',$data);
    }
            
	function add(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);
			if(empty($data)){
				$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not save'));	    			    		
			}
			else{
				$this->load->model('Menu_model');
				if(isset($data['id'])){
					$this->Menu_model->update($data,$data['id']);
					$data_return = $this->Menu_model->get_by_id($data['id']);
					$data_return['edit'] = '<a href="'.base_url().'admin/menu/edit/'.$data['id'].'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>';
					$jsonOutPut = json_encode(array('success'=>true,'message'=>'Saved successfully','data'=>$data_return));
				}	
				else{	
					$id = $this->Menu_model->insert($data);
					$data_return = $this->Menu_model->get_by_id($id);
					$data_return['edit'] = '<a href="'.base_url().'admin/menu/edit/'.$id.'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>';
					$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','data'=>$data_return));
				}		 
			}
		}catch (Exception $e){
			$jsonOutPut = json_encode(array('success'=>false,'message'=>'Can not save'));	
		}
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}	
	}
			
	function delete(){
		$callback = $_REQUEST['callback'];
		$jsonOutPut = '';
		try{
			$data = json_decode ($_GET['data'],true);			
			$this->load->model('Menu_model');
			$this->Menu_model->delete($data);
			$jsonOutPut =  json_encode(array('success'=>true));
		}
		catch (Exception $e){
			$jsonOutPut =  json_encode(array('success'=>false));
		}
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}	
	}
			
	function grid(){
		$this->load->model('Menu_model');
		$callback = $_REQUEST['callback'];
		$data =  array();
		foreach($this->Menu_model->get_list($_GET['start'],$_GET['limit'],(isset($_GET['sort']))?$_GET['sort']:'id',(isset($_GET['dir']))?$_GET['dir']:'DESC') as $row){
			$data[] = array_merge($row,array('edit'=>'<a href="'.base_url().'admin/menu/edit/'.$row['id'].'"><img src="'.base_url().'public/skin/back-end/images/edit.png"></a>'));
		}
		$jsonOutPut = json_encode(array('success'=>true,'message'=>'Loaded data','start'=>$_GET['start'],'total'=>$this->Menu_model->get_total(),'data'=>$data));
		if ($callback) {
			header('Content-Type: text/javascript');
			echo $callback . '(' . $jsonOutPut . ');';
		} else {
			header('Content-Type: application/x-json');
			echo $jsonOutPut;
		}
	}
			
	function edit($id=0)
	{
		$this->load->model('Menu_model');
		$this->load->model('Language');
		$this->load->library('helpfunc');
		if($this->input->server('REQUEST_METHOD') == 'POST'){
			$input_data = $_POST;
			if(!empty($id) && $this->Menu_model->update($input_data,$id)){
				die(json_encode(array('success'=>true,'id'=>$id)));
			}elseif($id = $this->Menu_model->insert($input_data,$id)){
				die(json_encode(array('success'=>true,'id'=>$id)));
			}
			else
				die(json_encode(array('success'=>false)));

		}
		$data = array('datacontent' => (empty($id))?array():$this->Menu_model->get_by_id($id));
		
		foreach($this->Language->get_all() as $row){
			$data['langList'][$row['language_code']] = $row['language_name'];
		}
		$this->_setLayout('admin/menu/menu_edit', 'admin_layout', $data);
	}
            
}
		 ?>